🌟 2.1 What is Selenium?
Selenium is an open-source automation tool used for testing web applications across different browsers and platforms. It mimics real user actions like clicking buttons, typing in forms, navigating links, etc.
✅ Why Selenium?
- Supports multiple programming languages: Python, Java, C#, Ruby
- Works on multiple browsers: Chrome, Firefox, Edge, Safari
- Can be integrated with test frameworks like Pytest, Unittest, etc.
- Supports parallel execution and CI/CD tools (e.g., Jenkins)
🧠 2.2 Selenium Architecture Overview
Selenium has four main components:
- Selenium IDE – A browser plugin for recording scripts.
- Selenium RC – Legacy tool (Deprecated).
- Selenium WebDriver – Modern, robust automation framework.
- Selenium Grid – For distributed test execution.
✅ 1. Selenium IDE
🛠 What is it?
Selenium IDE is a browser extension (for Chrome and Firefox) that allows recording, editing, and debugging of functional tests.
- Click “Record” → It captures user actions like clicking, typing, etc.
- It generates test cases in a tabular or script format.
- Tests can be replayed directly.
❌ 2. Selenium RC (Remote Control)
🛠 What is it?
Selenium RC was the original solution for automating browsers before WebDriver.
✅ Why deprecated: Required server setup, slower, replaced by WebDriver.
🚀 3. Selenium WebDriver – Modern Standard
Allows direct control of browsers through language-specific bindings.
[Test Script]
↓
[Selenium Client Library (Python)]
↓
[Browser Driver (e.g., ChromeDriver)]
↓
[Browser (e.g., Chrome)]
🌐 4. Selenium Grid
Allows parallel test execution across machines and browsers. Useful for CI/CD.
🛠️ 2.3 Installation & Environment Setup
✅ Step 1: Install Python
Download from https://python.org and ensure “Add Python to PATH” is checked.
python --version
✅ Step 2: Install Selenium Library
pip install selenium
🧪 2.4 First Selenium Test Script in Python
✅ Test Case: Open Google and search for "Selenium"
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
driver.get("https://www.google.com")
search_box = driver.find_element(By.NAME, "q")
search_box.send_keys("Selenium")
search_box.submit()
time.sleep(5)
driver.quit()
🔍 2.5 Locators in Selenium
Locators help Selenium find elements on a webpage.
Common Locator Strategies:
- ID –
find_element(By.ID, "username")
- Name –
find_element(By.NAME, "email")
- Class Name –
find_element(By.CLASS_NAME, "btn")
- Tag Name –
find_element(By.TAG_NAME, "input")
- Link Text –
find_element(By.LINK_TEXT, "Click Here")
- Partial Link Text –
find_element(By.PARTIAL_LINK_TEXT, "Click")
- XPath –
find_element(By.XPATH, "//input[@id='search']")
- CSS Selector –
find_element(By.CSS_SELECTOR, "input[name='q']")
✅ Examples of Each Locator
# ID
driver.find_element(By.ID, "username").send_keys("admin")
# Name
driver.find_element(By.NAME, "password").send_keys("admin123")
# Class Name
driver.find_element(By.CLASS_NAME, "login-button").click()
# Tag Name
all_links = driver.find_elements(By.TAG_NAME, "a")
for link in all_links:
print(link.text)
# Link Text
driver.find_element(By.LINK_TEXT, "Forgot Password?").click()
# Partial Link Text
driver.find_element(By.PARTIAL_LINK_TEXT, "Forgot").click()
🤔 Why use XPath?
- When ID or class names are missing or dynamic
- When elements are deeply nested
- When you want to select elements based on text, relationships, or conditions
🧠 Types of XPath
1️⃣ Absolute XPath ❌
It starts from the root of the HTML document and goes through each node until the desired element.
/html/body/div[1]/form/input[2]
– Brittle, not recommended
2️⃣ Relative XPath ✅
Uses double forward slashes to search anywhere in the document.
//tagname[@attribute='value']
Example:
driver.find_element(By.XPATH, "//input[@id='username']")
🔍 XPath Syntax Variants
//input[@name='email']
//input[contains(@name, 'user')]
//input[starts-with(@id, 'user')]
//button[text()='Submit']
//a[contains(text(), 'Login')]
🔗 XPath Axes
//input[@id='email']/parent::div
//label[text()='Username']/following-sibling::input
//input[@id='password']/preceding-sibling::label
//span[text()='Login']/ancestor::form
🛠 Real World Example
<div class="form">
<label for="email">Email:</label>
<input type="text" name="email" id="emailInput" />
</div>
Example 1:
driver.find_element(By.XPATH, "//input[@id='emailInput']")
Example 2:
driver.find_element(By.XPATH, "//label[text()='Email:']/following-sibling::input")
🚨 Common Mistakes
- ❌ Using absolute XPath
- ❌ Typos in tag/attribute
- ❌ Not escaping quotes
✅ Best Practices
- Use relative XPath
- Use contains()/starts-with()
- Avoid deep absolute paths
✅ XPath Summary Table
Syntax | Description | Example |
---|---|---|
//tag[@attr='value'] | Basic selector | //input[@id='user'] |
contains() | Partial match | //a[contains(text(),'Sign')] |
starts-with() | Start of attr | //input[starts-with(@id, 'user')] |
text() | Match text | //button[text()='Login'] |
parent::, ancestor:: | Traverse up | //input/parent::div |
following-sibling:: | Select sibling | //label/following-sibling::input |
📌 Sample Project Folder Structure
selenium-python-demo/
├── chromedriver.exe
├── first_test.py
└── requirements.txt
✅ requirements.txt
Install all dependencies:
pip install -r requirements.txt
Example contents:
selenium==4.18.1
pytest
requests
✅ Final Tip
To create requirements.txt:
pip freeze > requirements.txt
⬅️ Previous: 02-Chapter 2 Selenium With Python Fundamentals
➡️ Next: Chapter 3 – Selenium WebDriver Core Concepts">➡️ Next: Chapter 3 – Selenium WebDriver Core Concepts